const App = getApp();
Page({
  /**
   * 页面的初始数据
   */
  onShareAppMessage() {
    return {
      title: 'picker-view',
      path: 'page/component/pages/picker-view/picker-view'
    }
  },
  data: {
    isLogin: false,
    userbirthday:"2000-01-01", //生日
    add_date:"2010-01-01", //入会时间
    shanghui_id: 0,  
    shanghui_name:'',
    avatarUrl:"/images/default-avatar.jpg",
    identityList:[],
    index: 0,
    userSex:"1",  //性别
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(options.shanghui_id){
      this.setData({
        shanghui_id:options.shanghui_id,
        shanghui_name:options.name,
      })
    }
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let that = this;
    that.setData({
      isLogin: App.checkIsLogin()
    });
    if (that.data.isLogin) {
      console.log(that.data.shanghui_name)
      // 获取当前用户信息
     // that.getUserDetail();
     that.getIdentity()
    }
  },
  bindPickerChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },
  //性别
  radioChange(e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value)
    this.setData({
      userSex:e.detail.value
    })
  },
  // 身份列表
  getIdentity(){
    let that = this;
    App._get('apply/identity', {}, result => {
      console.log(result.data)
      that.setData({
        identityList: result.data.identity
      }); 
    });
  },
  //提交
  formSubmit(e) {
    var formData=e.detail.value;//获取表单数据
    let obj = {
      nickName:formData.userName, //姓名
      phone:formData.userTel,  //电话
      birth_date:this.data.userbirthday,//生日
      gender:this.data.userSex,//性别
      shanghui_id:this.data.shanghui_id, //商会id
      identity:this.data.identityList[this.data.index],
      add_date:this.data.add_date,//入会时间
      userNumber:formData.userNumber,//身份证
    };
    if(obj.nickName == ''){
      App.showError("姓名不能空！");
      return;
    }
    if(obj.phone.length <11){
      App.showError("请输入正确手机号！");
      return;
    }
    let _this = this;
    App._post_form('apply/add_user', obj, result => {
      //成功提示
      App.showSuccess(result.msg, function() {
        wx.navigateTo({
          url: '/pages/index/index',
        })
      });

    });
  },

// 头像
  onChooseAvatar(e) {
    const { avatarUrl } = e.detail;
    let av=e.detail.avatarUrl;
    let av1=wx.getFileSystemManager().readFileSync(av,'base64');
    let avatarUrl1 = "data:image/jpeg;base64,"+av1;
    this.setData({
      avatarUrl,
    })
    App._get('user.index/upUserInfo', {avatarUrl:avatarUrl1}, function(result) {
      console.log(result.data);
    });
  },
  //生日
  bindDateChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      userbirthday: e.detail.value
    })
  },
  //入会时间
  bindDateChange1: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      add_date: e.detail.value
    })
  },
})